require "public"

local POKER = {
    0x01,0x02,
    0x03,0x04,
    0x05,0x06,
    0x07,0x08,0x09, 
    0x0a,0x0b,
    0x0c,
    0x0d,  -- 方块  
	0x11,0x12,
	0x13,0x14,0x15,0x16,
	0x17,0x18,0x19, 
	0x1a,0x1b,
	0x1c,
	0x1d,  -- 梅花                       
    0x21,
    0x22,0x23,
    0x24,0x25,0x26,0x27,0x28,0x29, 0x2a,0x2b,0x2c,0x2d,  -- 红桃
    0x31,
    0x32,0x33,
    0x34,0x35,0x36, 0x37,0x38,0x39, 0x3a,0x3b,0x3c,0x3d,  -- 黑桃 
}



local times = { 
	[ 0		] = 1,   --无牛
	[ 1		] = 1,    --牛1
	[ 2		] = 1,  
	[ 3		] = 1,  
	[ 4		] = 1,  
	[ 5		] = 1,  
	[ 6		] = 1,  
	[ 7		] = 1,  
	[ 8	] = 2,  
	[ 9	] = 3,         --牛9
	[ 10	] = 4,     --牛牛
	[ 40	] = 6,      --四炸
	[ 50	] = 5,      --五花牛
	[ 51	] = 10,     --五小牛
}

local pkDealer = class("puke_dealer")
 

function pkDealer:reset_game(host_id )
	if host_id == 0 or host_id == nil then
		host_id = 1
	end
    self.cards = {}       
    self.host_id_ = host_id
  
	self.dibei_ = 1
	self.log_data = { cards = {}, players = {},zhanji_log = {}, room_info = {}, order = {} }
	self.jiesan_ = {}
	self.apply_jiesan_time = 0
	self.cur_host_id = 0
	self.hu_users_  = {} 
end
 
function pkDealer:get_niu_score(niu )
	if times[niu] == nil then
		times[niu] = 1
	end
	return times[niu]
end

function pkDealer:set_cur_host(host_id )
	self.cur_host_id = host_id 
end

function pkDealer:get_cur_host()
	return self.cur_host_id
end
 
function pkDealer:add_jiesan(index)
	self.jiesan_[index] = 1	
end

function pkDealer:reset_jiesan(curr_time)
	self.jiesan_ = {}
	if curr_time then 
		self.apply_jiesan_time = os.time()
	else
		self.apply_jiesan_time = 0
	end
end

function pkDealer:get_jiesan()
	local length = 0
	for i,v in pairs(self.jiesan_) do
		length = length + 1
	end
	return length,self.jiesan_
end

function pkDealer:get_jiesan_time()
	return  self.apply_jiesan_time
end

function pkDealer:set_host(id)
    self.host_id_ = id
end

function pkDealer:get_host()
    return self.host_id_
end

function pkDealer:set_dibei(flag)
	self.dibei_ = flag
end

function pkDealer:get_dibei()
	return self.dibei_
end 

--初始化手牌
function pkDealer:shuffle(max_cliend,gameType)
	math.randomseed(os.time())
    assert(type(POKER) == "table")
    local copy = {}
    for k, v in pairs(POKER) do  
        copy[k] = v
    end  
    local range = #POKER
    for k, v in pairs(POKER) do  
        local index = math.random(1, range)
        POKER[k] = table.remove(copy, index)
        range = range - 1
    end  

    self.rest_cards_ = clone(POKER)
    local max = 5
    if gameType == 131 then
    	max = 6
    end  
    for k=1,max_cliend do
    	self.cards[k] = {}
		for i=1,max do
	        self.cards[k][i] = table.remove(self.rest_cards_, 1) 
	    end 
    end 
end

--获取玩家手牌
function pkDealer:get_initial_card(i)
    return self.cards[i]
end

-- 获取花式
function pkDealer:GetColor( data ) 
    return data & 0xF0
end

-- 获取数值
function pkDealer:GetValue( data ) 
    return data & 0x0F
end
 
--判断玩家手牌类型
function pkDealer:check_card_type(  tbl,optionType ) 
	local card_max = 0
	local color_max = 0 
	local wuhuaniu = 1
	local wuxiaoniu = 0
	local zadan = 0
	local card_v = {}
	local card_all = {}
	local is_wuxiaoniu = string.sub(optionType,3,3)
	local is_zhadan = string.sub(optionType,2,2)
	local is_wuhuaniu = string.sub(optionType,1,1)
    for k,v in ipairs(tbl) do
    	local value = v & 0x0F
    	local color = v & 0xF0
    	if value < 5 and wuxiaoniu < 11 then
    		wuxiaoniu = wuxiaoniu + value
    	end
    	if value > 4 then
    		wuxiaoniu = 99
    	end
    	if value < 11 then
    		wuhuaniu = 0
    	end
    	if value > card_max then
			card_max = value 
			color_max = color 
    	end 
    	if is_zhadan == '1' then
			if not card_v[value] then
				card_v[value] = 1
			else
				card_v[value] = card_v[value] + 1
		    	if card_v[value] == 4 then
		    		zadan = 1 
		    	end
			end 
		end 
		if value > 9 then
			value = 10
		end
        table.insert(card_all,value)
    end 
    if is_wuxiaoniu == '1' and wuxiaoniu > 0 and wuxiaoniu < 11 then
    	return 51,card_max,color_max,tbl
    end 
    if is_zhadan == '1' and zadan == 1 then 
	    return 40,card_max,color_max,tbl 
    end 
    if is_wuhuaniu == '1' and wuhuaniu == 1  then
    	return 50,card_max,color_max,tbl
    end 
	local is_niu = 0
	local niu = 0
	local card_max_niu = 0
	local color_max_niu = 0 
	local hu_cards_sort = {0,0,0,0,0} 
	local max = 0  
	local last = 0  
	for i=1,3 do
		for ii=i+1,4 do
			for iii=ii+1,5 do
				if i ~= ii and i ~= iii and ii ~= iii then   
					if (card_all[i] + card_all[ii] + card_all[iii]) % 10 == 0 then  
						is_niu = 1
						max = 0 
						niu = 0 
						last = 0 
						for k=1,5 do
							if k ~= i and k ~= ii and k ~= iii then   
								if last == 0 then
									last =  tbl[k]
								end   
								niu = niu + card_all[k]  
								local aa = tbl[k] & 0x0F
								local color = tbl[k] & 0xF0
						    	if aa > card_max_niu or (aa == card_max_niu and color_max_niu < color) then 
									card_max_niu = aa  
									color_max_niu = color
									max = 1
									if hu_cards_sort[1] > 0 then
										hu_cards_sort = {0,0,0,0,0} 
									end 
        							hu_cards_sort[1] = tbl[i]
        							hu_cards_sort[2] = tbl[ii]
        							hu_cards_sort[3] = tbl[iii] 
        							hu_cards_sort[4] = tbl[k] 
						    	end     
								if max == 1 and hu_cards_sort[4] == last then
									last =  tbl[k]
								end   
							end  
						end
						if max == 1 and last ~= 0 then
							hu_cards_sort[5] =  last
						end 
						break
					end 
				end
		    end 
	    end 
    end   
    if is_niu == 1  then
    	local num = niu % 10
    	if num == 0 then
    		num = 10
    	end
    	return num,card_max,color_max,hu_cards_sort
    else
    	return 0,card_max,color_max,tbl
    end 
end
 
function pkDealer:show_rand_card(length)
	local index = math.random(1, length)
	return self.rest_cards_[index]
end
   
function pkDealer:add_hu_user(id)
	table.insert(self.hu_users_,id)
end

function pkDealer:get_hu_users()
	return self.hu_users_
end

function pkDealer:set_hu_id(id)
	self.hu_id = id
end

function pkDealer:get_hu_id()
	return self.hu_id
end

function pkDealer:get_hu_cards()
	return self.hu_cards_
end

function pkDealer:set_log_cards(cards)
	self.log_data.cards = cards
end

function pkDealer:set_log_players(players)
	self.log_data.players = players
end

function pkDealer:set_log_room_info(room_info)
	self.log_data.room_info = room_info
end
function pkDealer:set_log_zhanji_log(zhanji_logg)
	self.log_data.zhanji_log = zhanji_logg
end
function pkDealer:add_log_order(cmd)
	return table.insert(self.log_data.order,cmd)
end

function pkDealer:get_log_data()
	return self.log_data
end


return pkDealer
